package com.situ.web.servlet;

import java.awt.Checkbox;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;

import com.situ.web.common.ServerResponse;
import com.situ.web.entity.User;
import com.situ.web.service.IUserService;
import com.situ.web.service.impl.UserServiceImpl;
import com.situ.web.util.JSONUtil;

import net.sf.json.JSONObject;
@WebServlet("/user")
public class UserServlet extends HttpServlet{
	private IUserService userService = new UserServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "login":
			login(req,resp);
			break;
		case "logout":
			logout(req,resp);
			break;
		case "insert":
			insert(req,resp);
			break;

		default:
			break;
		}
	}
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		System.out.println(name);
		System.out.println(password);
		User user = new User( name, password);
		System.out.println("insert"+user);
		boolean isSuccess = userService.insert(user);
		ServerResponse serverResponse = null;
		if(isSuccess) {
			serverResponse = ServerResponse.createSuccess("注册成功");
		}else {
			serverResponse = ServerResponse.createError("注册失败");
		}
		JSONUtil.obj2Json(resp, serverResponse);
		//resp.sendRedirect("/JavaWeb1907/user?method=login");
	}
	private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		if (user != null) {
			// session.removeAttribute("user");
			session.invalidate();
			// 重定向到登录界面
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
		}
	}
	private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String checkCode = req.getParameter("checkCode");
		System.out.println(checkCode);
		String checkCodeSess = (String) req.getSession().getAttribute("checkCodeSession");
		System.out.println(checkCodeSess);
		if (""== checkCode||null == checkCode|| checkCode.equals(checkCodeSess) == false) {
			ServerResponse serverResponse = ServerResponse.createSuccess("验证码错误");
			JSONUtil.obj2Json(resp,serverResponse);
			return;
		}
		
		
		String userName = req.getParameter("name");
		String userPassword = req.getParameter("password");
		User user = userService.login(userName,userPassword);
		
		if (null != user) {// 登录成功
			HttpSession session = req.getSession();
			session.setAttribute("user", user);
			
			List<User> onLineUserList = (List<User>)session.getServletContext().getAttribute("onLineUserList");
			// 第一个用户登录时候这个list是null
			if (null == onLineUserList) {
				onLineUserList = new ArrayList<User>();
				getServletContext().setAttribute("onLineUserList", onLineUserList);
			}
			onLineUserList.add(user);
//			resp.sendRedirect(req.getContextPath() + "/student?mehtod=pageList");			
			ServerResponse serverResponse = ServerResponse.createSuccess("登录成功");
			JSONUtil.obj2Json(resp,serverResponse);
			
		} else {// 登录失败
			//resp.sendRedirect(req.getContextPath() + "/fail.jsp");
			ServerResponse serverResponse = ServerResponse.createError("用户名或密码错误");
			JSONUtil.obj2Json(resp,serverResponse);
		}
	}
}
